package com.study.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.study.services.DemantService;

@Controller
public class DemandController {
	@Autowired
	DemantService ds;
	/**
	 * 加载新增界面
	 * Wcx
	 * 
	 * @param dname
	 */
	@GetMapping("adddem")
	public ModelAndView addopp() {
		System.out.println("新建进来了");
		ModelAndView mv = new ModelAndView("beforeSale/demandadd");
		JSONObject jsonObject = new JSONObject();
		mv.addObject("adddemand", jsonObject);
		return mv;
	}
	
	/**
	 * 展示页面
	 * @return
	 */
	@GetMapping("demand")
	public ModelAndView deptAdd() {
		// @GetMapping没有带斜杠/，默认的视图是jsp/add.jsp页面
		ModelAndView mv = new ModelAndView("beforeSale/demand");
		System.out.println("来了老弟");
		return mv;
	}
	
	/**
	 * 新增数据传递
	 * @param theme
	 * @param custname
	 * @param state
	 * @param type
	 * @param linkman
	 * @param laiyuan
	 * @param users
	 * @param demand
	 * @param possbity
	 * @param exprice
	 * @param youxian
	 * @param stage
	 * @param exdate
	 * @return
	 */
	@PostMapping("adddemand")
	@ResponseBody
	public ModelAndView aa(String theme,String privoder,String custid,String opp,String degree,String content,String oppid,HttpServletRequest request) {
		System.out.println("我来了Post");
		String useid= request.getSession().getAttribute("useid").toString();
		boolean result = ds.insertdem( theme, privoder, custid, opp, degree, content,new Date(),useid);
		ModelAndView mv = new ModelAndView("/beforeSale/demand");
		List<JSONObject> list = ds.selectopp(opp);
		if (list.size()==0) {
			List<JSONObject> list1 = ds.selectopp1(opp);
			if (list1.size()==0) {
				List<JSONObject> list2 = ds.selectopp2(opp);
				
				if (list2.size()==0) {
					List<JSONObject> list3 = ds.selectopp3(opp);
					if (list3.size()!=0) {
						ds.updatedem(opp);
					}
					
				}else {
					ds.updatesolu(opp);
				}
		
			}else {
				ds.updatecom(opp);
			}
		}else {
			ds.updatedd(opp);
		}
		
		return mv;
	}


	@PostMapping("updatedemand")
	@ResponseBody
	public ModelAndView bb(String theme,String privoder,String custname,String opp,String degree,String content, String demid
		,HttpServletRequest request) {
		System.out.println("我来了Post修改信息");
		System.out.println(theme+" "+ privoder+" "+ custname+" "+opp+" "+ degree+" "+ content);
		
		
		String useid= request.getSession().getAttribute("useid").toString();
		boolean result = ds.updatedemand( theme,privoder, custname, opp, degree, content,demid,useid);
		ModelAndView mv = new ModelAndView("/beforeSale/demand");
		return mv;
	}
	/**
	 * 带条件的分页查询
	 * 
	 * @param input
	 * @param selects
	 * @param currentPage
	 * @param pagesize
	 * @param session
	 * @param request
	 * @return
	 */
	@PostMapping("beforeSale/TJdemand")
	@ResponseBody
	public String  getForm3(String input, String selects, int currentPage, int pagesize, HttpSession session,
			HttpServletRequest request) {
		Integer cusid = (Integer) session.getAttribute("cusview");
		String useid= request.getSession().getAttribute("useid").toString();
		List<JSONObject> list = ds.selectBySel(currentPage, pagesize, input, selects,useid,cusid);
		System.out.println(list.size() + "长度");
		System.out.println(input);
		PageInfo<JSONObject> pager = new PageInfo<JSONObject>(list);
		String json = JSONObject.toJSONString(pager);
        System.out.println(json);
        session.removeAttribute("cusview");

		return json;
	}

	/**
	 * 修改数据回显
	 * @param linid
	 * @return
	 */
	@GetMapping("updatedem")
	public ModelAndView updatelinkman(String id){
		System.out.println("进来了aa");
		System.out.println(id);
		ModelAndView mv = new ModelAndView("beforeSale/demandadd");
		System.out.println("修改" +id +" 进来了");
		JSONObject jsonObject = ds.selectByid(id);
		System.out.println(jsonObject);
		mv.addObject("adddemand",jsonObject);
		System.out.println("323");
		return mv;
	}
	
	
	
	

}
